<template>
  <div>

      <div style="margin-bottom:15px">
        <el-input v-model="params.name" style="width:200px" placeholder="请输入研究方向"></el-input>
        <el-button type="warning" style="margin-left:10px" @click="findBySearch()">查询</el-button>
        <el-button type="warning" style="margin-left:10px" @click="reset()">清空</el-button>
        <el-button type="primary" style="margin-left:10px" @click="add()" v-if="user.role === 'ROLE_ADMIN'">添加</el-button>
              <!-- <el-popconfirm title="确定删除这些数据吗？" @confirm="delBatch()">
                <el-button slot="reference" type="danger" style="margin-left:5px" v-if="user.role === 'ROLE_ADMIN'">批量删除</el-button>
              </el-popconfirm> -->
        <el-button type="danger" style="margin-left:5px" @click="delBatch()" v-if="user.role === 'ROLE_ADMIN'">批量删除</el-button>
        
      </div>
      <div>
        <el-table :data="tableData" style="width: 100%" ref="table" @selection-change="handleSelectionChange" :row-key="getRowKeys">
          <el-table-column ref="table" type="selection" width="55" align="center" :reserve-selection="true" v-if="user.role === 'ROLE_ADMIN'">

          </el-table-column>
          <el-table-column prop="name" label="研究方向" ></el-table-column>
          <el-table-column prop="description" label="具体研究方向" ></el-table-column>
          
          <el-table-column  label="操作">
            <template slot-scope="scope">
              <el-button type="primary" @click="edit(scope.row)" v-if="user.role === 'ROLE_ADMIN'">编辑</el-button>
              <!-- <el-popconfirm title="确定删除吗？" @confirm="del(scope.row.id)">
                <el-button slot="reference" type="danger" style="margin-left:5px">删除</el-button>
              </el-popconfirm> -->
              <el-button type="danger" style="margin-left:5px" @click="del(scope.row.id)" v-if="user.role === 'ROLE_ADMIN'">删除</el-button>
              <!-- <el-button type="success" @click="betutor(scope.row.id)" v-if="user.role !== 'ROLE_TUTOR'">become a tutor</el-button> -->
              
              <!-- <el-button type="danger">删除</el-button> -->
            </template>
          </el-table-column>
        </el-table>
      </div>

            <div style="margin-top: 10px;">
          
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="params.pageNum"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="params.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
          </el-pagination>
        </div>

        <!-- 添加学科分类 -->
              <div>

                    <el-dialog title="Please fill in the information" :visible.sync="dialogFormVisible" width="65%">
            <el-form :model="form">
              <el-form-item label="category name" label-width="15%">
                <el-input v-model="form.name" autocomplete="off" style="width:90%"></el-input>
              </el-form-item>
              <el-form-item label="category description" label-width="15%">
                <el-input v-model="form.description" autocomplete="off" style="width:90%"></el-input>
              </el-form-item>
              
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="dialogFormVisible = false">cancel</el-button>
              <el-button type="primary" @click="submit()">confirm</el-button>
            </div>
          </el-dialog>

        </div>
    <!-- 成为家教 -->
        <div>

            <el-dialog title="Please fill in the information" :visible.sync="tutorVisible" width="70%">
            <el-form :model="admin">
             

              <el-form-item label="tutor image" label-width="15%">
                <el-upload action="http://localhost:8080/api/files/upload" :on-success="successUpload">
                  <el-button size="small" type="primary">Click to upload</el-button> 
                           
                </el-upload>
              
              </el-form-item>


              <el-form-item label="tutor price" label-width="15%">
                <el-input v-model="admin.price" autocomplete="off" style="width:90%"></el-input>
              </el-form-item>

              <el-form-item label="AliPay Account" label-width="15%">
                <el-input  v-model="AliPayAccount" autocomplete="off" style="width:90%"></el-input>
              </el-form-item>

              <el-form-item label="Wechat Account" label-width="15%">
                <el-input  v-model="WechatAccount" autocomplete="off" style="width:90%"></el-input>
              </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="tutorVisible = false">cancel</el-button>
              <el-button type="primary" @click="submitTutor()">confirm</el-button>
            </div>
          </el-dialog>

        </div>

  </div>
</template>

<script>
import request from '@/utils/request'
    export default {
      data() {
        return {
          params:{
            name:'',
            pageNum:1,
            pageSize:5
          },
          tableData: [],
          total: 0,
          dialogFormVisible: false,
          tutorVisible: false,
          form:{},
          admin:{},
          multipleSelection:[],
          AliPayAccount:'',
          WechatAccount:'',
          user:localStorage.getItem("user")?JSON.parse(localStorage.getItem("user")):{}
        }
      },
      //页面加载的时候，做一些事情，在created里面
      created(){
         this.findBySearch()
      },
      //定义一些页面上空间触发的事件调用的方法
      methods:{
        
        findBySearch(){
          request.get("/type/search",{
            params:this.params
          }).then(res =>{
            if(res.code === '0'){
              this.tableData = res.data.list
              this.total = res.data.total
            }else{

            }
          })
        },
        add(){
          this.form = {}
          this.dialogFormVisible = true
        },
        edit(obj){
          this.dialogFormVisible = true
          this.form = obj

        },
        reset(){
           this.params={
            pageNum:1,
            pageSize:5,
            name:'',
           }
           this.findBySearch()
        },
        handleSizeChange(pageSize){
           this.params.pageSize = pageSize
           this.findBySearch()
        },
        handleCurrentChange(pageNum){
          this.params.pageNum = pageNum
           this.findBySearch()
        },
        submit(){
          request.post("/type",this.form).then(res =>{
            if(res.code === '0'){
                this.$message.success("操作成功")
                this.dialogFormVisible = false
                this.findBySearch()
            }else{
                this.$message.error(res.msg)
            }
          })
        },
        del(id){
          this.$confirm("This operation will permanently delete the record. Do you want to continue？","prompt",{
            confirmButtonText: 'confirm',
            cancelButtonText: 'cancel',
            type: 'warning'
            }).then(() =>{
              request.delete("/type/"+id).then(res =>{
              if(res.code === '0'){
                this.$message.success("Delete successfully")
                this.findBySearch()
              }else{
                this.$message.error(res.msg)
              }
            })
            })
            .catch(() =>{
              this.$message.info("Cancelled deletion")
            })
          
        },
        delBatch(){
          if(this.multipleSelection.length ===0){
            this.$message.warning("Please check the item you want to delete")
            return
          }
          this.$confirm("This operation will permanently delete the record. Do you want to continue?","prompt",{
            confirmButtonText: 'confirm',
            cancelButtonText: 'cancel',
            type: 'warning'
            }).then(() =>{
              request.put("/type/delBatch",this.multipleSelection).then(res =>{
                if(res.code === '0'){
                  this.$message.success("Batch deletion successful")
                  this.findBySearch()
                }else{
                  this.$message.error(res.msg)
                }
              })
            }).catch(() =>{
              this.$message.info("Cancelled deletion")
            })
          
        },
        handleSelectionChange(val){
          this.multipleSelection = val;
        },
        getRowKeys(row){
          return row.id
        },
        betutor(typeId){
           this.tutorVisible = true;
           this.admin.typeId = typeId
           
        },
        successUpload(res){
           console.log(res);
           this.admin.img = res.data;
        },
        submitTutor(){
          this.admin.id = this.user.id
          this.admin.role = 'ROLE_TUTOR'
           request.put("/admin/betutor",this.admin).then(res =>{
            if(res.code === '0'){
                this.$message.success("Successfully become a tutor")
                let user = localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {}
                user['role'] = 'ROLE_TUTOR'
                localStorage.setItem("user", JSON.stringify(user))
                this.tutorVisible = false
                location.reload()
                this.findBySearch()
            }else{
                this.$message.error(res.msg)
            }
          })
        }

      }
    }
  </script>